package com.bao.work.jin.service.impl;

import com.bao.work.jin.entity.Goods;
import com.bao.work.jin.service.GoodsService;

import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author baoye
 * @date 2024-08-02/17:08
 */
public class GoodsServiceImpl implements GoodsService {
    static {
        try {
            init(GOODS_TREE,GOODS_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            init(CART_TREE,CART_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void init(Map<Goods,Integer> Map,String name) throws Exception {
        //读取文件,GoodsList中的内容到集合中
        BufferedReader reader = new BufferedReader(new FileReader("day09/src/com/bao/work/jin/service/impl/"+name));
        String goods;
        while ((goods = reader.readLine())!=null) {
            final String[] g = goods.split(SPLIT);
            Map.put(new Goods(g[0], g[1], new BigDecimal(g[2]), g[3], Integer.parseInt(g[4])), Integer.parseInt(g[5]));
        }
        reader.close();

    }
    public void addGoodsList(Goods goods,Integer num,String name) throws IOException {
        BufferedWriter writer=new BufferedWriter(new FileWriter("day09/src/com/bao/work/jin/service/impl/"+name,true));
        writer.write(goods.toString(SPLIT,num));
        writer.flush();
        writer.close();
    }

    public void updateTxt(Map<Goods,Integer> Map,String name) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter("day09/src/com/bao/work/jin/service/impl/"+name));
        for (Goods goods : Map.keySet()) {
            String goodsString = goods.toString(SPLIT, Map.get(goods));
            char[] chars = goodsString.toCharArray();
            writer.write(chars);
            writer.flush();
        }
        writer.close();
    }



    @Override
    public void addGoods() throws IOException {
        System.out.println("请输入商品编号");
        String goodsId = INPUT.next();
        INPUT.nextLine();
        System.out.println("请输入商品名称");
        String goodsName = INPUT.nextLine();
        System.out.println("请输入商品价格");
        BigDecimal price = new BigDecimal(INPUT.next());
        System.out.println("请输入商品颜色");
        String color = INPUT.next();
        System.out.println("请输入商品尺寸");
        int size = INPUT.nextInt();
        System.out.println("请输入商品库存");
        int num = INPUT.nextInt();
        Goods goods = new Goods(goodsId, goodsName, price, color, size);
        if (GOODS_TREE.put(goods, num) == null) {
            System.out.println("添加成功");
            addGoodsList(goods,num,GOODS_NAME);
        }else{
            System.out.println("内容被修改");
            updateTxt(GOODS_TREE,GOODS_NAME);
        }

    }

    @Override
    public void selectGoods() {
        GOODS_TREE.keySet().forEach(s -> System.out.println(s + "库存:" + GOODS_TREE.get(s)));
    }

    @Override
    public void selectGoodsById() {
        System.out.println("请输入需要查找的商品编号");
        String id = INPUT.next();
        for (Goods goods : GOODS_TREE.keySet()) {
            if (goods.getGoodsId().equals(id)) {
                System.out.println(goods);
                return;
            }
        }
        System.out.println("您查找的商品不存在");
    }

    @Override
    public void insertGoodsToCart() throws IOException {
        System.out.println("请输入您要添加的商品编号");
        Goods addGoods = new Goods();
        String id = INPUT.next();
        System.out.println("请输入购买数量");
        Integer num = 0;
        for (Goods goods : GOODS_TREE.keySet()) {
            if (goods.getGoodsId().equals(id)) {
                num = INPUT.nextInt();
                if (num > GOODS_TREE.get(goods)) {
                    System.out.println("超过库存");
                    return;
                }
                addGoods = goods;
                GOODS_TREE.put(goods, GOODS_TREE.get(goods) - num);
            }
        }

        if (addGoods.getGoodsId() == null) {
            System.out.println("商品编号不存在");
            return;
        }
        if (CART_TREE.get(addGoods) == null) {

            CART_TREE.put(addGoods, num);
            addGoodsList(addGoods,num,CART_NAME);
        } else {
            CART_TREE.put(addGoods, CART_TREE.get(addGoods) + num);
            updateTxt(CART_TREE,CART_NAME);
        }
        System.out.println("添加成功");
        updateTxt(GOODS_TREE,GOODS_NAME);


    }

    @Override
    public void selectCart() {
        CART_TREE.keySet().forEach(s -> System.out.println(s + "购买数量:" + CART_TREE.get(s)));
    }

}
